package com.panshi.controller;


import com.panshi.IZxUncheckApi;
import com.panshi.domain.ZxUncheck;
import com.panshi.domain.vo.PageBean;
import com.panshi.domain.vo.ZxResult;
import com.panshi.mapper.ZxArticleMapper;
import com.panshi.service.ZxArticleService;
import com.panshi.service.ZxUncheckService;
import com.panshi.vo.CodeMsgConstant;
import com.panshi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器 㔿
 * </p>
 *
 * @author zhy
 * @since 2020-07-29
 */
@CrossOrigin
@Slf4j
@Api(tags = "资讯未审核")
@RestController
@RequestMapping("/api/uncheck")
public class ZxUncheckController implements IZxUncheckApi {
    @Resource
    private ZxUncheckService zxUncheckService;
    @Resource
    private ZxArticleService zxArticleService;



    @PostMapping("/UncheckAll")
    @ApiOperation("后台查询所有未审核的文章")
    public R getUncheckAll(@RequestBody PageBean pageBean) {

        List<ZxUncheck> result = zxUncheckService.getUncheckAll(pageBean);
        if (result.size() > 0) {
            return R.ok(CodeMsgConstant.CODE_0).data("data", result).data("count", zxUncheckService.count());
        } else {
            return R.ok(CodeMsgConstant.CODE_1);
        }
    }


    @ApiOperation("根据ID查询未审核资讯")
    @GetMapping("/Uncheck/{id}")
    public R getUncheckZxBayId(@PathVariable("id") Integer id) {
        ZxUncheck zxUncheck = zxUncheckService.getUncheckZxBayId(id);
        if (zxUncheck != null) {
            return R.ok(CodeMsgConstant.CODE_0).data("zxUncheck", zxUncheck);
        } else {
            return R.ok(CodeMsgConstant.CODE_1);
        }
    }

    @ApiOperation("新增未审核资讯")
    @PostMapping("/addZxUncheck")
    public ZxResult addZxUncheck(@RequestBody ZxUncheck zxUncheck) {
        int n = zxUncheckService.addUncheckZX(zxUncheck);
        if (n > 0) {
            ZxUncheck uncheck = zxUncheckService.getUncheckZxBayId(zxUncheck.getId());
            zxUncheckService.dynamic(uncheck);//维护用户动态
            return new ZxResult(200, "插入数据成功", n);
        } else {
            return new ZxResult(400, "插入记录失败");
        }
    }

    @ApiOperation("文章过审,更新描述,修改审核")
    @PutMapping("/uncheckUp")
    public R uncheckUp(@RequestBody ZxUncheck zxUncheck) {
        int result = zxUncheckService.uncheckUp(zxUncheck);
        zxUncheckService.dynamic(zxUncheck);//维护用户动态
        if (result > 0 )  {
            if(zxUncheck.getStatus()==2){
                zxArticleService.addZxArticle(zxUncheck);//更新资讯通过表
                zxArticleService.addZxArticleSearch(zxArticleService.getZxArticleBayId(zxUncheck.getId()));//同步搜索表
            }
            return R.ok(CodeMsgConstant.CODE_0).data("zxUncheck", zxUncheck);
        } else {
            return R.error(CodeMsgConstant.CODE_1);
        }
    }


    @ApiOperation("删除未审核资讯")
    @DeleteMapping("/deleteUncheck/{id}")
    public R deleteUncheck(@PathVariable("id") Integer id) {
        int result = zxUncheckService.deleteUncheck(id);
        zxArticleService.userDelete(id);
        if (result > 0) {
            return R.ok(CodeMsgConstant.CODE_0);
        } else {
            return R.error(CodeMsgConstant.CODE_1);
        }

    }


}

